/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.flood.building;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.GenericAnswerListBox;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.flood.FloodConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.FloodBuildingDesign;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class FloodBuildingForm extends AbstractForm implements BuildingUpdate {
	
	//ConstructionClassListBox
	
	private Label postFIRMConstruction;
	private Label constructionClass;
	private Label buildingCondition;
	private Label substructureType;
	private Label lowestFloorElevation;
	private Label topBottomFloor;
	private Label lowestFloorBelowGrade;
	private Label mechanicalEquipmentHeight;
	
	// form fields
	private GenericAnswerListBox formPostFIRMConstruction = new GenericAnswerListBox();
	private BuildingConditionListBox formBuildingCondition = new BuildingConditionListBox();
	private ConstructionClassListBox formConstructionClass = new ConstructionClassListBox();
	private FloodSubstructureTypeListBox formSubstructureType = new FloodSubstructureTypeListBox();
	private TextBox formLowestFloorElevation = new TextBox();
	private TextBox formTopBottomFloor = new TextBox();
	private GenericAnswerListBox formLowestFloorBelowGrade = new GenericAnswerListBox();
	private TextBox formMechanicalEquipmentHeight = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(8,2);
	
	// entities
	private FloodBuildingDesign floodBuildingDesign;
	
	// listeners
	private FloodBuildingDesignListBoxChangeListener floodBuildingDesignListBoxChangeListener;
	private FloodBuildingDesignTextBoxKeyListener floodBuildingDesignTextBoxKeyListener;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private FloodConstants constants;
	
	public FloodBuildingForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (FloodConstants)GWT.create(FloodConstants.class);
		tabTitle = constants.floodBuildingTabTitle();
		
		postFIRMConstruction = new Label(constants.floodBuildingPostFIRMConstruction());
		constructionClass = new Label(constants.floodBuildingConstructionClass());
		buildingCondition = new Label(constants.floodBuildingBuildingCondition());
		substructureType = new Label(constants.floodBuildingSubstructureType());
		lowestFloorElevation = new Label(constants.floodBuildingLowestFloorElevation());
		topBottomFloor = new Label(constants.floodBuildingTopBottomFloor());
		lowestFloorBelowGrade = new Label(constants.floodBuildingLowestFloorBelowGrade());
		mechanicalEquipmentHeight = new Label(constants.floodBuildingMechanicalEquipmentHeight());
		
		// add labels to form
		table.setWidget(0, 0, postFIRMConstruction);
		table.setWidget(1, 0, constructionClass);
		table.setWidget(2, 0, buildingCondition);
		table.setWidget(3, 0, substructureType);
		table.setWidget(4, 0, lowestFloorElevation);
		table.setWidget(5, 0, topBottomFloor);
		table.setWidget(6, 0, lowestFloorBelowGrade);
		table.setWidget(7, 0, mechanicalEquipmentHeight);
		
		// set up form fields
		floodBuildingDesign = building.getFloodBuildingDesign();
		floodBuildingDesignListBoxChangeListener = new FloodBuildingDesignListBoxChangeListener(floodBuildingDesign);
		floodBuildingDesignTextBoxKeyListener = new FloodBuildingDesignTextBoxKeyListener(floodBuildingDesign);
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formPostFIRMConstruction);
		table.setWidget(1, 1, formConstructionClass);
		table.setWidget(2, 1, formBuildingCondition);
		table.setWidget(3, 1, formSubstructureType);
		table.setWidget(4, 1, formLowestFloorElevation);
		table.setWidget(5, 1, formTopBottomFloor);
		table.setWidget(6, 1, formLowestFloorBelowGrade);
		table.setWidget(7, 1, formMechanicalEquipmentHeight);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);			
		
	}

	public void updateBuilding(Building building) {
		
		// entities
		setBuilding(building);
		this.floodBuildingDesign = building.getFloodBuildingDesign();
		
		// listeners
		floodBuildingDesignListBoxChangeListener.updateFloodBuildingDesign(floodBuildingDesign);
		floodBuildingDesignTextBoxKeyListener.updateFloodBuildingDesign(floodBuildingDesign);
		
		// reload forms 
		loadForms();
	}
	
	private void loadForms()
	{
		if( floodBuildingDesign != null)
		{
			formPostFIRMConstruction.setListItem(floodBuildingDesign.getPostFIRMConstruction());
			formPostFIRMConstruction.setName(FloodBuildingDesign.POSTFIRMCONSTRUCTION);
			
			//TODO: Construction class is enabled ONLY if occupancy class is RES1
			formConstructionClass.setListItem(floodBuildingDesign.getConstructionClass());
			formConstructionClass.setName(FloodBuildingDesign.CONSTRUCTIONCLASS);
			
			formBuildingCondition.setListItem(floodBuildingDesign.getBuildingCondition());
			formBuildingCondition.setName(FloodBuildingDesign.BUILDINGCONDITION);
			
			formSubstructureType.setListItem(floodBuildingDesign.getSubstructureType());
			formSubstructureType.setName(FloodBuildingDesign.SUBSTRUCTURETYPE);
			
			String lowestFloorElevation = null;
			if (floodBuildingDesign.getLowestFloorElevation() != null) {
				lowestFloorElevation = floodBuildingDesign.getLowestFloorElevation().toString();
			}
			formLowestFloorElevation.setText( lowestFloorElevation);
			formLowestFloorElevation.setName(FloodBuildingDesign.LOWESTFLOORELEVATION);
			
			formTopBottomFloor.setText(floodBuildingDesign.getTopBottomFloor());
			formTopBottomFloor.setName(FloodBuildingDesign.TOPBOTTOMFLOOR);
			
			formLowestFloorBelowGrade.setListItem(floodBuildingDesign.getLowestFloorBelowGrade());
			formLowestFloorBelowGrade.setName(FloodBuildingDesign.LOWESTFLOORBELOWGRADE);
			
			String mechanicalEquipmentHeight = null;
			if (floodBuildingDesign.getMechanicalEquipmentHeight() != null) {
				mechanicalEquipmentHeight = floodBuildingDesign.getMechanicalEquipmentHeight().toString();
			}
			formMechanicalEquipmentHeight.setText( mechanicalEquipmentHeight );
			formMechanicalEquipmentHeight.setName(FloodBuildingDesign.MECHANICALEQUIPMENTHEIGHT);
		}
        
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formPostFIRMConstruction.setEnabled(false);
	            formConstructionClass.setEnabled(false);
	            formBuildingCondition.setEnabled(false);
	            formSubstructureType.setEnabled(false);
	            formLowestFloorElevation.setEnabled(false);
	            formTopBottomFloor.setEnabled(false);
	            formLowestFloorBelowGrade.setEnabled(false);
	            formMechanicalEquipmentHeight.setEnabled(false);
	        }
	        else
	        {
	            formPostFIRMConstruction.setEnabled(true);
	            formConstructionClass.setEnabled(true);
	            formBuildingCondition.setEnabled(true);
	            formSubstructureType.setEnabled(true);
	            formLowestFloorElevation.setEnabled(true);
	            formTopBottomFloor.setEnabled(true);
	            formLowestFloorBelowGrade.setEnabled(true);
	            formMechanicalEquipmentHeight.setEnabled(true);            
	        }
		}
	}
	
	private void loadListeners()
	{
		formPostFIRMConstruction.addChangeListener(floodBuildingDesignListBoxChangeListener);
		formConstructionClass.addChangeListener(floodBuildingDesignListBoxChangeListener);
		formBuildingCondition.addChangeListener(floodBuildingDesignListBoxChangeListener);
		formSubstructureType.addChangeListener(floodBuildingDesignListBoxChangeListener);
		
		formLowestFloorElevation.addFocusListener(floodBuildingDesignTextBoxKeyListener);
		formLowestFloorElevation.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formTopBottomFloor.addFocusListener(floodBuildingDesignTextBoxKeyListener);
		formLowestFloorBelowGrade.addChangeListener(floodBuildingDesignListBoxChangeListener);
		
		formMechanicalEquipmentHeight.addFocusListener(floodBuildingDesignTextBoxKeyListener);
		formMechanicalEquipmentHeight.addBlurHandler(textBoxIntegerValueBlurHandler);
		
	}

}
